﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ajayumi.Platform.WinForm.UserCtlPanel;
using System.Threading;

namespace ajayumi.Test.WinUI
{
    public partial class DetailDemoPanel : UserCtlPanelDetailBase
    {

        public DetailDemoPanel()
        {
            InitializeComponent();

            this.RunWorkerCompleted += (sender, e) =>
            {
                this.txtInfo.AppendText(string.Format("{0} {1} 异步处理完成。\r\n", DateTime.Now, e.UserState));

                if (e.Error != null)
                {
                    this.txtInfo.AppendText(string.Format("{0} 进行异步处理时，出现异常。异常信息如下：\r\n{1}\r\n",
                        DateTime.Now, e.Error.Message));

                }
            };
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.RunWorkAsync();
        }

        //protected override void InitWorkers()
        //{
        //    this.Workers.Add("ajayumi1", (asyncOp) =>
        //    {
        //        this.UpdateUI(asyncOp, () =>
        //        {
        //            this.txtInfo.AppendText(string.Format("{0} ajayumi1 开始异步处理。\r\n", DateTime.Now));
        //        });
        //        Thread.Sleep(1000);
        //        for (int i = 1; i <= 50; i++)
        //        {
        //            Thread.Sleep(1000);
        //            if (this.TaskCanceled("ajayumi1"))
        //            { break; }
        //            this.UpdateUI(asyncOp, () =>
        //            {
        //                this.txtInfo.AppendText(string.Format("{0} ajayumi1 output: {1}\r\n", DateTime.Now, i));
        //            });
        //        }
        //    });

        //    this.Workers.Add("ajayumi2", (asyncOp) =>
        //    {
        //        this.UpdateUI(asyncOp, () =>
        //        {
        //            this.txtInfo.AppendText(string.Format("{0} ajayumi2 开始异步处理。\r\n", DateTime.Now));
        //        });
        //        Thread.Sleep(1000);
        //        for (int i = 1; i <= 50; i++)
        //        {
        //            Thread.Sleep(2000);
        //            if (this.TaskCanceled("ajayumi2"))
        //            { break; }
        //            this.UpdateUI(asyncOp, () =>
        //            {
        //                this.txtInfo.AppendText(string.Format("{0} ajayumi2 output: {1}\r\n", DateTime.Now, i));
        //            });
        //        }
        //    });
        //    this.Workers.Add("ajayumi3", (asyncOp) =>
        //    {
        //        this.UpdateUI(asyncOp, () =>
        //        {
        //            this.txtInfo.AppendText(string.Format("{0} ajayumi3 开始异步处理。\r\n", DateTime.Now));
        //        });
        //        Thread.Sleep(1000);
        //        for (int i = 1; i <= 50; i++)
        //        {
        //            Thread.Sleep(3000);
        //            if (this.TaskCanceled("ajayumi3"))
        //            { break; }
        //            this.UpdateUI(asyncOp, () =>
        //            {
        //                this.txtInfo.AppendText(string.Format("{0} ajayumi3 output: {1}\r\n", DateTime.Now, i));
        //            });
        //        }
        //    });
        //}


        private void button1_Click(object sender, EventArgs e)
        {
            this.CancelAsync();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.RunWorkAsync();
        }
    }
}
